package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.Product;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.mapper.ProductMapper;
import com.woniuxy.factory.service.ProductService;
import com.woniuxy.factory.util.AuditType;
import com.woniuxy.factory.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service("productService")
public class ProductServiceImp implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Override
    public ResponseResult<List<Product>> getProductAll(int page, int limit,Product product) {
        int count = productMapper.getProductCount(product);
        //计算分页的起始条数
        int home = (page-1)*limit;
        List<Product> products = null;
        try {
            products = productMapper.selectProductsPage(home,limit,product);
        }catch (RuntimeException e){

            return new ResponseResult<>(AuditType.ALREADY_PASSED,"系统维护中",null,null);
        }
        return new ResponseResult<>(AuditType.No_Approval,"查询成功",count,products);
    }
    //获取满足指定条件的所有商品信息
    @Override
    public ResponseResult<List<Product>> getProducts(Product product) {
        ResponseResult<List<Product>> responseResult = new ResponseResult<List<Product>>();
        List<Product> products = productMapper.selectProducts(product);
        responseResult.setData(products);
        return responseResult;
    }

    @Override
    public ResponseResult addproduct(Product product) {
        product.setPrdId(UuidUtil.get16UUID());
        int i=0;
        try {
            i = productMapper.insertProduct(product);
        }catch (Exception e){
            throw new BusinessException("系统维护中");
        }
        if (i==0){
            throw new BusinessException("新增失败");
        }
        return new ResponseResult(AuditType.No_Approval,"新增成功",null,null);
    }

    @Override
    public ResponseResult updateproduct(Product product) {
        //验证商品是否存在
        Product product1 = new Product();
        product1.setPrdId(product.getPrdId());
        List<Product> products = productMapper.selectProducts(product1);
        if (products.size()==0){
            throw new BusinessException("该商品不存在");
        }
        int i = productMapper.updateProduct(product);
        if (1==0){
            throw new BusinessException("修改失败");
        }
        return new ResponseResult(AuditType.No_Approval,"修改成功",null,null);
    }

    @Override
    public ResponseResult deleteProduct(String prdId) {
        //验证商品是否存在
        Product product = new Product();
        product.setPrdId(prdId);
        List<Product> products = productMapper.selectProducts(product);
        if (products.size()==0){
            throw new BusinessException("该商品不存在");
        }
        int i =productMapper.deleteProduct(prdId);
        if (i==0){
            return new ResponseResult(AuditType.ALREADY_PASSED,"删除失败",null,null);
        }
        return new ResponseResult(AuditType.No_Approval,"删除成功",null,null);
    }

    @Override
    public ResponseResult deleteProducts(String[] prdIds) {
        int i = productMapper.deleteProducts(prdIds);
        if (i==0){
            throw new BusinessException("删除失败");
        }
        return new ResponseResult(AuditType.No_Approval,"删除成功",null,null);
    }
}
